package com.shanghaionstar.carmanage.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.mag.enumtype.PackageType;
import com.onstar.cn.more.entity.PackageInfo;
import com.shanghaionstar.R;

import java.util.List;

/**
 * @author Xiaofeng Feng
 * @ClassName: PayMeOrderAdapter
 * @Description: PayMeOrderAdapter
 * @date 2015年9月9日 下午2:29:07get
 */
public class PayMeOrderAdapter extends CommonAdapter<PackageInfo> {

    private static final String TAG = "PayMeOrderAdapter";
    private List<PackageInfo> groupList;
    private Context context;
    private int position = -1;
    private int selectPosition = -1;
    private PackageInfo selectPackInfo;
    //	private String year = "1";
    private PackageType packageType;
    /*定义一个类CallBack的引用mCallBack，可根据需要生成getter/setter mCallback引用（指针）负责调用PayMeOrderActivity中的onDataClick(int p)函数*/
    private Callback mCallback;

    /*定义一个类也叫抽象类特殊的抽象类也叫接口，负责调用PayMeOrderActivity中的onDataClick(int p)函数*/
    public interface Callback {
        void onDataClick(int p);
    }

    /**
     * @Description: PayMeOrderAdapter
     * @author Xiaofeng Feng
     * @date 2015年9月9日 下午3:20:54
     */
    public PayMeOrderAdapter(List<PackageInfo> groupList, Context context, PackageType packageType, Callback cb) {
        super(groupList, context);
        this.groupList = groupList;
        this.context = context;
        this.packageType = packageType;
        this.mCallback = cb;
    }

    @Override
    public int getCount() {
        if (groupList != null) {
            return groupList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (groupList != null && position < groupList.size()) {
            return groupList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public int getSelectPosition() {
        return selectPosition;
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public PackageInfo getSelectPackInfo() {
        return selectPackInfo;
    }

    public void setSelectPackInfo(PackageInfo selectPackInfo) {
        this.selectPackInfo = selectPackInfo;
    }
    //	public void setYear(String year) {
//		this.year = year;
//	}

    public String degreeCode;

    public void setDegreeCode(String degreeCode) {
        this.degreeCode = degreeCode;
    }


    private class ViewHolder extends CommonViewHolder {
        @FindViewById
        private Button tv_order_name;
    }

    /**
     * (non-Javadoc)
     * <p>Title: getView</p>
     * <p>Description: </p>
     *
     * @param
     * @param viewHolder
     * @see com.onstar.cn.framework.base.CommonAdapter#getView(int, com.onstar.cn.framework.base.CommonViewHolder)
     */
    @Override
    public void getView(final int groupPosition, CommonViewHolder viewHolder) {

        ViewHolder holder = (ViewHolder) viewHolder;
        if (packageType == PackageType.DATA || groupList.get(groupPosition).getDegreeCode().equals(degreeCode)) {
            holder.tv_order_name.setText(groupList.get(groupPosition).getPackageName());
            // 选中的套餐包信息
            if (selectPosition == groupPosition) {
                holder.tv_order_name.setBackgroundResource(R.drawable.sel_bg);
                mCallback.onDataClick(groupPosition); //yongqiang tao
            } else {
                holder.tv_order_name.setBackgroundResource(R.drawable.nor_sel_bg);
            }
            holder.tv_order_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectPackInfo = groupList.get(groupPosition);
                    Log.i(TAG, selectPackInfo.toString());
                    selectPosition = groupPosition;
                    notifyDataSetChanged();
                }
            }); //抢夺焦点 yongqiang tao

        }
    }

    /**
     * (non-Javadoc)
     * <p>Title: layoutId</p>
     * <p>Description: </p>
     *
     * @return
     * @see com.onstar.cn.framework.base.CommonAdapter#layoutId()
     */
    @Override
    public int layoutId() {
        return R.layout.onstar_payme_order_list_group_item;

    }

    /**
     * (non-Javadoc)
     * <p>Title: getViewHolder</p>
     * <p>Description: </p>
     *
     * @return
     * @see com.onstar.cn.framework.base.CommonAdapter#getViewHolder()
     */
    @Override
    public CommonViewHolder getViewHolder() {
        // TODO Auto-generated method stub
        return new ViewHolder();
    }

}
